﻿using AutoMapper;
using B.S.Data.MES.API.Applications.GenerateItemsApplication;
using B.S.Data.MES.API.Dto.GenerateIDto;
using B.S.Requst;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.Data.MES.API.Controllers
{
    /// <summary>
    /// 生产计划物品API
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GenerateItemsController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<GenerateItemsController> _logger;

        public GenerateItemsController(IMediator mediator, ILogger<GenerateItemsController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 分页获取生产计划物品列表
        /// </summary>
        /// <param name="request">查询条件</param>
        /// <returns>物品列表</returns>
        [HttpGet]
        public async Task<APIPageing<List<DtoGenerateItems>>> ShowGenerateItems([FromQuery] ShowGenerateItemsCommand request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 根据ID获取单个生产计划物品
        /// </summary>
        /// <param name="id">物品ID</param>
        /// <returns>物品详情</returns>
        [HttpGet("{id}")]
        public async Task<APIRequst<DtoGenerateItems>> GetGenerateItemById(int id)
        {
            var command = new GetGenerateItemByIdCommand { GenerateItemsId = id };
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 添加生产计划物品
        /// </summary>
        /// <param name="request">物品信息</param>
        /// <returns>添加结果</returns>
        [HttpPost]
        public async Task<APIRequst<int>> AddGenerateItem([FromBody] AddGenerateItemsCommand request)
        {
            try
            {
                return await _mediator.Send(request);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "添加生产计划物品失败");
                return new APIRequst<int>
                {
                    Code = RequstCode.失败,
                    Msg = $"添加失败：{ex.Message}",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 更新生产计划物品
        /// </summary>
        /// <param name="request">物品信息</param>
        /// <returns>更新结果</returns>
        [HttpPut]
        public async Task<APIRequst<int>> UpdateGenerateItem([FromBody] UpdateGenerateItemsCommand request)
        {
            try
            {
                return await _mediator.Send(request);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "更新生产计划物品失败");
                return new APIRequst<int>
                {
                    Code = RequstCode.失败,
                    Msg = $"更新失败：{ex.Message}",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 删除生产计划物品
        /// </summary>
        /// <param name="id">物品ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete("{id}")]
        public async Task<APIRequst<int>> DeleteGenerateItem(int id)
        {
            try
            {
                var command = new DeleteGenerateItemsCommand { GenerateItemsId = id };
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除生产计划物品失败");
                return new APIRequst<int>
                {
                    Code = RequstCode.失败,
                    Msg = $"删除失败：{ex.Message}",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 批量删除生产计划物品
        /// </summary>
        /// <param name="request">物品ID列表</param>
        /// <returns>删除结果</returns>
        [HttpPost]
        public async Task<APIRequst<int>> BatchDeleteGenerateItems([FromBody] BatchDeleteGenerateItemsCommand request)
        {
            try
            {
                return await _mediator.Send(request);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "批量删除生产计划物品失败");
                return new APIRequst<int>
                {
                    Code = RequstCode.失败,
                    Msg = $"批量删除失败：{ex.Message}",
                    Data = 0
                };
            }
        }

        /// <summary>
        /// 根据基础计划ID获取物品列表
        /// </summary>
        /// <param name="basicId">基础计划ID</param>
        /// <returns>物品列表</returns>
        [HttpGet("{basicId}")]
        public async Task<APIPageing<List<DtoGenerateItems>>> GetItemsByBasicId(int basicId)
        {
            var command = new ShowGenerateItemsCommand 
            { 
                GenerateplanBasicId = basicId,
                PageIndex = 1,
                PageSize = 100 // 获取该计划下的所有物品
            };
            return await _mediator.Send(command);
        }
    }
}
